home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Networking / SNMP / SNMP Development / MacSNMP Developer 1.0.2 / Library Manager Interfaces / LibraryManagerClasses.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-15  |  65.1 KB  |  2,537 lines  |  [TEXT/MPS ]

  1. /*    File:        LibraryManagerClasses.h
  2.  
  3.     Contains:    Declarations for LibraryManager classes
  4.  
  5.     Copyright:    © 1991-1992 by Apple Computer, Inc., all rights reserved.
  6.  
  7.  
  8. */
  9.  
  10. #ifndef __LIBRARYMANAGERCLASSES__
  11. #define __LIBRARYMANAGERCLASSES__
  12.  
  13. #ifndef __LIBRARYMANAGER__
  14. #include <LibraryManager.h>
  15. #endif
  16.  
  17. /*******************************************************************************
  18. ** Forward class declarations
  19. ********************************************************************************/
  20.  
  21. class TFormattedStream;    // not actually defined yet
  22. class TSemaphore;
  23.  
  24. class TMatchObject;
  25. class THashObject;
  26. class TIterator;
  27. class TCollection;
  28. class TLink;
  29. class TPriorityLink;
  30. class TSimpleList;
  31. class TLinkedList;
  32. class TPriorityList;
  33. class TListIterator;
  34. class TArray;
  35. class TArrayIterator;
  36.  
  37. class TOperation;
  38. class TScheduler;
  39. class TPriorityScheduler;
  40. class TSerialScheduler;
  41. class TThreadScheduler;
  42. class TTaskScheduler;
  43. class TInterruptScheduler;
  44. class TTimeScheduler;
  45.  
  46. class TNotifier;
  47. class TProcNotifier;
  48. class TMethodNotifier;
  49.  
  50. class TMemoryPool;
  51. class TStandardPool;
  52. class TChunkyPool;
  53. class TGrowOperation;
  54. class TPoolNotifier;
  55.  
  56. class TArbitrator;
  57. class TTokenNotification ;
  58. class TToken;
  59. class TRequestToken;
  60.  
  61. class TClassInfo;
  62. class TLibraryFile;
  63.  
  64. class TBitmap;
  65. class TDoubleLong;
  66. class THashDoubleLong;
  67. class TTime;
  68. class TMicroseconds;
  69. class TMilliseconds;
  70. class TSeconds;
  71. class TTimeStamp;
  72. class TStopwatch;
  73.  
  74. class TTraceLog;
  75.  
  76. class TException ;
  77.  
  78. /*******************************************************************************
  79. ** Some typedefs
  80. ********************************************************************************/
  81.  
  82. typedef unsigned long    EventCode;
  83. typedef void*            GlobalWorld;
  84.  
  85. typedef void            (*ProcessProc)(TOperation*);
  86.  
  87. // Below is a typedef for a pointer to a notify method for an object. Ignore the
  88. // fact the the typedef claims that it needs to be a TDynamic method. It can be
  89. // any kind of class. The only caveat is that if the NotifyProc is a virtual
  90. // function, the vtable for the object must be the first field of the object.
  91. // You can force the vtable to be first by creating a base class that has atleast
  92. // one virtual function and no data members, like TDynamic does.
  93. typedef void            (TDynamic::*NotifyMethod)(EventCode, OSErr, void*);
  94. typedef void            (*NotifyProc)(void* refPtr, EventCode, OSErr, void*);
  95.  
  96. /*******************************************************************************
  97. ** Miscellaneous constant
  98. ********************************************************************************/
  99.  
  100. #ifndef kInvalidWorld
  101. #define kInvalidWorld        ((GlobalWorld)0)
  102. #endif
  103.  
  104. /*******************************************************************************
  105. ** Some constants needed for Notification
  106. ********************************************************************************/
  107.  
  108. const EventCode kTokenNotification    = 0x40000001;
  109. const EventCode    kLowPoolMemoryEvent    = 0x40000002;
  110. const EventCode    kHighPoolMemoryEvent= 0x40000003;
  111. const EventCode    kDownsizePoolEvent    = 0x40000004;
  112.  
  113. /*******************************************************************************
  114. ** Some "C" Global routines
  115. ********************************************************************************/
  116.  
  117. extern "C"
  118. {
  119.     TStandardPool*    GetLocalPool();
  120.     void            SetLocalPool(TStandardPool*);
  121.     TStandardPool*    GetClientPool();
  122.     TStandardPool*    GetDefaultPool();
  123.     void            SetDefaultPool(TStandardPool*);
  124.     TStandardPool*    GetSystemPool();
  125.     
  126.     TTraceLog*        GetGlobalTraceLog();
  127.     void            SetGlobalTraceLog(TTraceLog*);
  128.     void            Trace(const char *formatStr, ...);
  129.  
  130.     TArbitrator*    GetGlobalArbitrator();
  131.     TTaskScheduler*    GetGlobalTaskScheduler();
  132. };
  133.  
  134. /*******************************************************************************
  135. ** TSemaphore class
  136. **
  137. ** Since threads don't really exist on the Macintosh, Semaphores are
  138. ** done by shutting interrupts off to guarantee exclusivity.  Therefore,
  139. ** the rule is - never hold a semaphore very long, since on the Macintosh
  140. ** it will degrade performance if you do.
  141. ********************************************************************************/
  142.     
  143. #define kTSemaphoreID "!$sema"
  144.  
  145. class TSemaphore : public TDynamic
  146. {
  147.     public:
  148.                             TSemaphore();
  149.         virtual                ~TSemaphore();
  150.     
  151.         virtual void        Grab();
  152.         virtual void        Release();
  153.         virtual Boolean        GrabNoWait();
  154.  
  155.     private:
  156.                             TSemaphore(const TSemaphore&);
  157.                 void        operator=(const TSemaphore&);
  158.  
  159.         short    fSaveLevel;
  160.         short    fCount;
  161. };
  162.  
  163. /*******************************************************************************
  164. ** CLASS TMatchObject
  165. **
  166. ** This object is the base class for any object which "knows" how
  167. ** to hash a specific object, as well as how to compare a
  168. ** 2nd object to the specific object.
  169. ********************************************************************************/
  170.  
  171. #define kTMatchObjectID "!$mobj"
  172.  
  173. class TMatchObject : public TDynamic
  174. {
  175.     public:
  176.         virtual                    ~TMatchObject();
  177.         
  178.             // Default implementation is to return 0
  179.         virtual unsigned long    Hash() const;
  180.             // Default implementation is to compare
  181.             // address of "this" with address of the object
  182.         virtual short            Compare(const void*) const;
  183.             // Default implementation is to call Compare
  184.         virtual Boolean            IsEqual(const void*) const;
  185.  
  186.     protected:
  187.                                 TMatchObject();
  188. };
  189.  
  190. /*******************************************************************************
  191. ** CLASS THashObject
  192. **
  193. ** This object is the base class for any object which "knows" how
  194. ** to hash another object.
  195. ********************************************************************************/
  196.  
  197. #define kTHashObjectID "!$hobj"
  198.  
  199. class THashObject : public TDynamic
  200. {
  201.     public:
  202.         virtual                    ~THashObject();
  203.         
  204.         virtual    unsigned long    Hash(const void*) const    = 0;
  205.  
  206.     protected:
  207.                                 THashObject();
  208. };
  209.  
  210. /*******************************************************************************
  211. ** CLASS TIterator
  212. **
  213. ** This class Iterates through a collection of objects.  Since
  214. ** collections are "thread-safe", when the Next() method returns
  215. ** NULL, call IterationComplete().  If it returns true, then you were
  216. ** returned NULL because the iterator was done.  Otherwise, you were
  217. ** returned NULL because the underlying collection changed.  
  218. ** RemoveCurrentObject will return false if the collection changed
  219. ** before the remove could be done.
  220. ********************************************************************************/
  221.  
  222. #define kTIteratorID "!$iter"
  223.  
  224. class TIterator : public TDynamic
  225. {
  226.     public:
  227.         virtual                ~TIterator();
  228.         
  229.         virtual    void        Reset()                        = 0;
  230.         virtual void*        Next()                        = 0;
  231.         
  232.         virtual Boolean        IterationComplete() const    = 0;
  233.         virtual Boolean        RemoveCurrentObject()        = 0;
  234.         
  235.                 void             SetMatchObject(TMatchObject* theMatcher);
  236.                 TMatchObject*     GetMatchObject() const;
  237.  
  238.     protected:
  239.                             TIterator();
  240.     private:
  241.         TMatchObject*        fMatcher;
  242. };
  243.  
  244. /*    -------------------------------------------------------------------------
  245.     inline methods for TIterator
  246.     ------------------------------------------------------------------------- */
  247.     inline void TIterator::SetMatchObject(TMatchObject* theMatcher)
  248.     {
  249.         fMatcher = theMatcher;
  250.     }
  251.     
  252.     inline TMatchObject* TIterator::GetMatchObject() const
  253.     {
  254.         return fMatcher;
  255.     }
  256.  
  257. /*******************************************************************************
  258. ** CLASS TCollection
  259. **
  260. ** This class defines the framework for all collections.
  261. ********************************************************************************/
  262.  
  263. #define kTCollectionID "!$coll"
  264.  
  265. class TCollection : public TDynamic
  266. {
  267.     public:
  268.         virtual                ~TCollection();
  269.         
  270.                 size_t        Count() const;
  271.                 Boolean        IsEmpty() const;
  272.         virtual TIterator*    CreateIterator(TStandardPool*) const        = 0;
  273.         
  274.         virtual OSErr        Add(void*);
  275.         virtual OSErr        AddUnique(void*, const TMatchObject&);
  276.         virtual OSErr        AddUnique(void*);
  277.                 
  278.         virtual void        RemoveAll();
  279.         virtual void        DeleteAll(Boolean isTDynamic = true);    
  280.         virtual void*        Remove(const TMatchObject&)                    = 0;
  281.         virtual void*        Member(const TMatchObject&)                    = 0;
  282.         virtual Boolean        Remove(void*)                                = 0;
  283.         virtual Boolean        Member(const void*)                            = 0;
  284.         
  285.         virtual void*        GetIndexedObject(size_t) const;
  286.                 void*        operator[](size_t);
  287.  
  288.                 long        GetSeed() const;
  289.                 void        Grab();
  290.                 void        Release();
  291.     
  292.     protected:
  293.                             TCollection();
  294.         virtual OSErr        PrivateAdd(void*, long)                        = 0;
  295.         virtual void        KillAll(Boolean ifDel, Boolean isTDynamic = true)    = 0;
  296.  
  297.         long        fSeed;
  298.         size_t        fCount;
  299.         TSemaphore    fSemaphore;
  300. };
  301.  
  302. /*    -----------------------------------------------------------------
  303.     Inline Methods for TCollection
  304.     ----------------------------------------------------------------- */
  305.  
  306.     inline size_t TCollection::Count() const
  307.     {
  308.         return fCount;
  309.     }
  310.  
  311.     inline Boolean TCollection::IsEmpty() const
  312.     {
  313.         return fCount == 0;
  314.     }
  315.  
  316.     inline void* TCollection::operator[](size_t idx)
  317.     {
  318.         return GetIndexedObject(idx);
  319.     }
  320.  
  321.     inline long TCollection::GetSeed() const
  322.     {
  323.         return fSeed;
  324.     }
  325.     
  326.     inline void TCollection::Grab()
  327.     {
  328.         (&fSemaphore)->Grab();
  329.     }
  330.     
  331.     inline void TCollection::Release()
  332.     {
  333.         (&fSemaphore)->Release();
  334.     }
  335.     
  336. /*******************************************************************************
  337. ** CLASS TLink
  338. **
  339. ** This class implements a link object which can be placed on a linked
  340. ** list.  It is totally non-virtual since it is a trivial class and
  341. ** to keep it at 8 bytes in size.
  342. ********************************************************************************/
  343.  
  344. class TLink
  345. {
  346.     public:
  347.                         TLink(void* value);
  348.                         TLink(TLink* link, void* value);
  349.                         TLink(Boolean);
  350.                         TLink();
  351.                         ~TLink();
  352.                         
  353.         void*            operator new(size_t size, TMemoryPool*);    // default size, from a pool
  354.         void*            operator new(size_t);                        // from default pool
  355.         void            operator delete(void* mem) {SLMDeleteOperator(mem);};
  356.         
  357.         void            SetNext(TLink* link);
  358.         TLink*            GetNext() const;
  359.         
  360.         void*            GetValue() const;
  361.         void            SetValue(void*);
  362.         
  363.         void            Append(TLink* newLink);        // append newLink after this
  364.         void            Remove(TLink* previous);    // remove nextLink from list
  365.  
  366.     private:
  367.         TLink*        fNext;
  368.         void*        fValue;
  369. };
  370.  
  371. /*    -----------------------------------------------------------------
  372.     Inline Methods for TLink
  373.     ----------------------------------------------------------------- */
  374.     
  375.     inline void* TLink::operator new(size_t size, TMemoryPool* thePool)
  376.     {
  377.         return SLMNewOperator(size, thePool);
  378.     }
  379.     
  380.     inline void* TLink::operator new(size_t size)
  381.     {
  382.         return SLMNewOperator(size, NULL);
  383.     }
  384.  
  385.     inline TLink::TLink(Boolean)
  386.     {}
  387.     
  388.     inline TLink::TLink()
  389.     {
  390.         fNext    = NULL;
  391.         fValue    = NULL;
  392.     }
  393.     
  394.     inline TLink::TLink(void* value)
  395.     {
  396.         fNext    = NULL;
  397.         fValue    = value;
  398.     }
  399.     
  400.     inline TLink::TLink(TLink* link, void* value)
  401.     {
  402.         fNext    = link;
  403.         fValue    = value;
  404.     }
  405.     
  406.     inline TLink::~TLink()
  407.     {
  408.         fNext    = NULL;
  409.         fValue    = NULL;
  410.     }
  411.     
  412.     inline void TLink::SetNext(TLink* link)
  413.     {
  414.         fNext = link;
  415.     }
  416.         
  417.     inline TLink* TLink::GetNext() const
  418.     {
  419.         return fNext;
  420.     }
  421.         
  422.     inline void* TLink::GetValue() const
  423.     {
  424.         return fValue;
  425.     }
  426.     
  427.     inline void TLink::SetValue(void* obj) 
  428.     {
  429.         fValue = obj;
  430.     }
  431.     
  432.     inline void TLink::Append(TLink* newLink)
  433.     {
  434.         newLink->SetNext(fNext);
  435.         fNext = newLink;
  436.     }
  437.     
  438.     inline void TLink::Remove(TLink* previous)
  439.     {
  440.         TLink* link = previous->GetNext();
  441.         previous->SetNext(fNext);
  442.         link->SetNext(NULL);
  443.     }
  444.     
  445. /*******************************************************************************
  446. ** CLASS TPriorityLink
  447. **
  448. ** This class implements a link object which can be placed on a linked
  449. ** list, and can hold a timer or priority value.
  450. ********************************************************************************/
  451.  
  452. const unsigned long kNormalPriority        = ((unsigned long)-1L) >> 1;
  453. const unsigned long kHighestPriority    = 0;
  454. const unsigned long    kLowestPriority        = (unsigned long)-1L;
  455. const unsigned long    kToLowerPriority    = 1;
  456.  
  457. class TPriorityLink : public TLink
  458. {
  459.     public:
  460.                         TPriorityLink(Boolean);
  461.                         TPriorityLink(void* value);
  462.                         TPriorityLink(TLink* link, void* value);
  463.                         TPriorityLink();
  464.                         
  465.         void            SetPriority(unsigned long);
  466.         unsigned long    GetPriority() const;
  467.     
  468.     private:
  469.         unsigned long    fPriority;
  470. };
  471.  
  472. /*    -----------------------------------------------------------------
  473.     Inline Methods for TPriorityLink
  474.     ----------------------------------------------------------------- */
  475.     
  476.     inline TPriorityLink::TPriorityLink(Boolean val) : TLink(val)
  477.     {}
  478.     
  479.     inline TPriorityLink::TPriorityLink()
  480.     {
  481.         fPriority = kNormalPriority;
  482.     }
  483.     
  484.     inline TPriorityLink::TPriorityLink(TLink* link, void* value) : 
  485.         TLink(link, value)
  486.     {
  487.         fPriority = kNormalPriority;
  488.     }
  489.     
  490.     inline TPriorityLink::TPriorityLink(void* value) : TLink(value)
  491.     {
  492.         fPriority = kNormalPriority;
  493.     }
  494.     
  495.     inline void TPriorityLink::SetPriority(unsigned long pri)
  496.     {
  497.         fPriority = pri;
  498.     }
  499.         
  500.     inline unsigned long TPriorityLink::GetPriority() const
  501.     {
  502.         return fPriority;
  503.     }
  504.     
  505. /*******************************************************************************
  506. ** CLASS TSimpleList
  507. **
  508. ** This class implements a simple linked list, which can have objects added
  509. ** at the front or the back of the list.
  510. ********************************************************************************/
  511.  
  512. #define kTSimpleListID "!$slst"
  513.  
  514. class TSimpleList : public TCollection
  515. {
  516.     friend class    TListIterator;
  517.  
  518.     public:
  519.                                 TSimpleList();
  520.                                 TSimpleList(TMemoryPool*);
  521.                                 TSimpleList(TSimpleList*);
  522.         virtual                    ~TSimpleList();
  523.     
  524.         // TCollection overrides
  525.         
  526.         virtual TIterator*        CreateIterator(TStandardPool*) const;
  527.         
  528.         virtual void*            Remove(const TMatchObject&);
  529.         virtual void*            Member(const TMatchObject&);
  530.         virtual Boolean            Remove(void*);
  531.         virtual Boolean            Member(const void*);
  532.         
  533.         // New methods
  534.         
  535.         virtual TLink*            MemberLink(const void*);
  536.         virtual TLink*            MemberLink(const TMatchObject&);
  537.         virtual TLink*            RemoveLink(void*);
  538.         virtual TLink*            RemoveLink(const TMatchObject&);
  539.  
  540.         virtual TLink*            FirstLink() const;
  541.         virtual TLink*            LastLink() const;
  542.         virtual    TLink*            RemoveFirstLink();
  543.         virtual    TLink*            RemoveLastLink();
  544.         virtual    void            AddLinkFirst(TLink*);
  545.         virtual    void            AddLinkLast(TLink*);
  546.  
  547.         virtual    void*            First() const;
  548.         virtual    void*            Last() const;
  549.         virtual    void*            RemoveFirst();
  550.         virtual    void*            RemoveLast();
  551.         virtual    OSErr            AddFirst(void*);
  552.         virtual    OSErr            AddLast(void*);
  553.         
  554.                 void            SetLinkPool(TMemoryPool*);
  555.                 TMemoryPool*    GetLinkPool() const;
  556.  
  557.     protected:
  558.         virtual OSErr            PrivateAdd(void*, long);
  559.         virtual void            KillAll(Boolean ifDel, Boolean isTDynamic = true);
  560.         virtual TLink*            PrivateFind(const void*, const TMatchObject*, TLink**) const;
  561.  
  562.     private:
  563.                                 TSimpleList(const TSimpleList&);
  564.                 void            operator=(const TSimpleList&);
  565.         
  566.     protected:
  567.     
  568.         TLink*            fList;
  569.         TLink*            fLastInList;
  570.         TMemoryPool*    fLinkPool;
  571. };
  572.  
  573. /*    -----------------------------------------------------------------
  574.     Inline Methods for TSimpleList
  575.     ----------------------------------------------------------------- */
  576.     
  577.     inline void TSimpleList::SetLinkPool(TMemoryPool* thePool)
  578.     {
  579.         fLinkPool = thePool;
  580.     }
  581.  
  582.     inline TMemoryPool* TSimpleList::GetLinkPool() const
  583.     {
  584.         return fLinkPool;
  585.     }
  586.  
  587. /*******************************************************************************
  588. ** CLASS TLinkedList
  589. **
  590. ** This class adds the ability to do things with a linked list based on
  591. ** "after" or "before" rules.
  592. ********************************************************************************/
  593.  
  594. #define kTLinkedListID "slm:coll$llst"
  595.  
  596. class TLinkedList : public TSimpleList
  597. {
  598.     public:
  599.                                 TLinkedList();
  600.                                 TLinkedList(TMemoryPool*);
  601.                                 TLinkedList(TSimpleList*);
  602.         virtual                    ~TLinkedList();
  603.             
  604.         // New methods
  605.                 
  606.         virtual    void*            After(const void* obj) const;
  607.         virtual    void*            After(const TMatchObject&) const;
  608.         virtual    void*            Before(const void* obj) const;
  609.         virtual    void*            Before(const TMatchObject&) const;
  610.         
  611.         virtual Boolean            AddLinkAfter(TLink*, const TMatchObject&);
  612.         virtual Boolean            AddLinkAfter(TLink*, const void* obj);
  613.         virtual Boolean            AddLinkBefore(TLink*, const TMatchObject&);
  614.         virtual Boolean            AddLinkBefore(TLink*, const void* obj);
  615.         virtual OSErr            AddAfter(void*, const TMatchObject&);
  616.         virtual OSErr            AddAfter(void*, const void* obj);
  617.         virtual OSErr            AddBefore(void*, const TMatchObject&);
  618.         virtual OSErr            AddBefore(void*, const void* obj);
  619.  
  620.     private:
  621.                                 TLinkedList(const TLinkedList&);
  622.                 void            operator=(const TLinkedList&);
  623. };
  624.  
  625. /*******************************************************************************
  626. ** CLASS TPriorityList
  627. **
  628. ** This class implements a list where objects are sorted in order of a 
  629. ** priority.
  630. ********************************************************************************/
  631.  
  632. #define kTPriorityListID "!$plst"
  633.  
  634. class TPriorityList : public TSimpleList
  635. {
  636.     public:
  637.                                 TPriorityList();
  638.                                 TPriorityList(TMemoryPool*);
  639.                                 TPriorityList(TPriorityList*);
  640.         virtual                    ~TPriorityList();
  641.     
  642.         // TLinkedList overrides
  643.         
  644.         virtual    OSErr            AddFirst(void*);
  645.         virtual    OSErr            AddLast(void*);
  646.         virtual    void            AddLinkFirst(TLink*);
  647.         virtual    void            AddLinkLast(TLink*);
  648.         
  649.         // New methods
  650.         
  651.         virtual    OSErr            AddPrioritized(void*, unsigned long pri);
  652.         virtual    void            AddLink(TPriorityLink*);
  653.  
  654.     private:
  655.                                 TPriorityList(const TPriorityList&);
  656.                 void            operator=(const TPriorityList&);
  657.         virtual OSErr            PrivateAdd(void*, long);
  658. };
  659.     
  660. /*******************************************************************************
  661. ** CLASS TListIterator
  662. **
  663. ** This iterator is used to iterate all collection classes descending from
  664. ** TSimpleList.
  665. ********************************************************************************/
  666.  
  667. #define kTListIteratorID "!$litr"
  668.  
  669. class TListIterator : public TIterator
  670. {
  671.     public:
  672.                             TListIterator(TSimpleList*);
  673.         virtual                ~TListIterator();
  674.         
  675.         virtual TLink*        GetCurrentLink() const;
  676.  
  677.         // TIterator Overrides
  678.         
  679.         virtual    void        Reset();
  680.         virtual void*        Next();
  681.                 
  682.         virtual Boolean        IterationComplete() const;
  683.         virtual Boolean        RemoveCurrentObject();
  684.         
  685.         // New methods
  686.                 void        SetList(TSimpleList*);
  687.                 
  688.     private:
  689.                             TListIterator(const TListIterator&);
  690.                 void        operator=(const TListIterator&);
  691.  
  692.         virtual    TLink*        NextLink();
  693.                 
  694.         TSimpleList*    fList;
  695.         TLink*            fPrevLink;
  696.         TLink*            fCurLink;
  697.         long            fSeed;
  698.         Boolean            fComplete;
  699. };
  700.  
  701. /*    -------------------------------------------------------------------------
  702.     Inline methods for TListIterator
  703.     ------------------------------------------------------------------------- */
  704.  
  705.     inline void TListIterator::SetList(TSimpleList* list)
  706.     {
  707.         fList = list;
  708.         Reset();
  709.     }
  710.  
  711. /*******************************************************************************
  712. ** CLASS TArray
  713. **
  714. ** This class implements an array collection, where objects can be efficiently
  715. ** looked at by index, and are added to the end of the array.  Deleting objects
  716. ** moves all higher-indexed objects down by 1 index number.
  717. ********************************************************************************/
  718.  
  719. #define kTArrayID "slm:coll$arry"
  720.  
  721. class TArray : public TCollection
  722. {
  723.     friend class    TArrayIterator;
  724.  
  725.     public:
  726.                                 TArray();
  727.                                 TArray(size_t size, TStandardPool* = NULL, 
  728.                                        short growBy = 0);
  729.         virtual                    ~TArray();
  730.                 
  731.                  TStandardPool*    GetGrowPool() const;
  732.                 
  733.         // TCollection overrides
  734.         
  735.         virtual TIterator*        CreateIterator(TStandardPool*) const;
  736.         
  737.         virtual Boolean            Remove(void*);
  738.         virtual void*            Remove(const TMatchObject&);
  739.         virtual Boolean            Member(const void*);
  740.         virtual void*            Member(const TMatchObject&);
  741.         
  742.         virtual void*            GetIndexedObject(size_t) const;
  743.         
  744.     private:
  745.                                 TArray(const TArray&);
  746.                 void            operator=(const TArray&);
  747.                 void            InitArray(size_t size, TStandardPool*, short growBy);
  748.         
  749.     protected:
  750.         virtual void*            MemberIndex(const void*, const TMatchObject*, size_t*);
  751.         virtual void*            PrivateRemove(void*, const TMatchObject*);
  752.         virtual OSErr            PrivateAdd(void*, long);
  753.         virtual void            KillAll(Boolean ifDel, Boolean isTDynamic = true);
  754.         virtual Boolean            Grow();
  755.  
  756.         void**            fArray;        // memory allocated for the array
  757.         size_t            fSize;        // current size of the array
  758.         long            fGrowBy;    // size to grow by if array is full
  759. };
  760.     
  761. /*******************************************************************************
  762. ** CLASS TArrayIterator
  763. **
  764. ** This class Iterates through a TArray collection
  765. ********************************************************************************/
  766.  
  767. #define kTArrayIteratorID "slm:coll$aitr"
  768.  
  769. class TArrayIterator : public TIterator
  770. {
  771.     public:
  772.                             TArrayIterator(TArray*);
  773.         virtual                ~TArrayIterator();
  774.         
  775.         virtual    void        Reset();
  776.         virtual void*        Next();
  777.         
  778.         virtual Boolean        IterationComplete() const;
  779.         virtual Boolean        RemoveCurrentObject();
  780.  
  781.     private:
  782.                             TArrayIterator(const TArrayIterator&);
  783.                 void        operator=(const TArrayIterator&);
  784.  
  785.     private:
  786.         TArray*        fArray;
  787.         size_t        fCurIdx;
  788.         long        fSeed;
  789.         Boolean        fComplete;
  790. };
  791.  
  792. /**********************************************************************
  793. ** CLASS TOperation
  794. ***********************************************************************/
  795.  
  796. #define kTOperationID    "!$oper"
  797.  
  798. class TOperation : public TDynamic
  799. {
  800.     public:
  801.                                 TOperation();
  802.                                 TOperation(long creatorData);
  803.                                 TOperation(void* creatorPtr);
  804.                                 TOperation(ProcessProc, long creatorData);
  805.                                 TOperation(ProcessProc, void* creatorPtr);
  806.                                 TOperation(const TOperation&);
  807.         virtual                    ~TOperation();
  808.         
  809.                 TPriorityLink*    GetLink();
  810.                 
  811.         virtual void            Reset();
  812.         virtual void            Process();
  813.         
  814.         // New Methods
  815.         
  816.                 void            SetProcessProc(ProcessProc);
  817.                 ProcessProc        GetProcessProc() const;
  818.  
  819.                 // Timer and Priority are just 2 different ways
  820.                 // of looking at the same field.
  821.                 
  822.                 void            SetTime(const TTime&);
  823.                 void            SetTime(unsigned long msecs);
  824.                 void            SetPriority(unsigned long pri);
  825.                 unsigned long    GetTime() const;
  826.                 unsigned long    GetPriority() const;
  827.  
  828.                 // CreatorData and CreatorPtr are just 2 different ways
  829.                 // of looking at the same field.
  830.                 
  831.                 void*            GetCreatorPtr() const;
  832.                 long            GetCreatorData() const;
  833.                 void            SetCreatorPtr(void*);
  834.                 void            SetCreatorData(long);
  835.                 
  836.                 GlobalWorld        GetSavedGlobalWorld() const;
  837.                 void            SetSavedGlobalWorld(GlobalWorld);
  838.         
  839.     private:
  840.         
  841.         ProcessProc                fProc;
  842.         union
  843.         {
  844.             void*        fPtr;
  845.             long        fLong;
  846.         }                        fCreatorData;            // Storage for the Creator
  847.         TPriorityLink            fProcessLink;
  848.         GlobalWorld                fSavedWorld;
  849. };
  850.  
  851. /*    -----------------------------------------------------------------
  852.     INLINE Methods for TOperation
  853.     ----------------------------------------------------------------- */
  854.     
  855.     inline TPriorityLink* TOperation::GetLink()
  856.     {
  857.         return &fProcessLink;
  858.     }
  859.     
  860.     inline unsigned long TOperation::GetTime() const
  861.     {
  862.         return fProcessLink.GetPriority();
  863.     }
  864.     
  865.     inline unsigned long TOperation::GetPriority() const
  866.     {
  867.         return fProcessLink.GetPriority();
  868.     }
  869.     
  870.     inline void TOperation::SetTime(unsigned long msec)
  871.     {
  872.         fProcessLink.SetPriority(msec);
  873.     }
  874.     
  875.     inline void TOperation::SetProcessProc(ProcessProc proc)
  876.     {
  877.         fProc = proc;
  878.     }
  879.     
  880.     inline void TOperation::SetPriority(unsigned long pri)
  881.     {
  882.         fProcessLink.SetPriority(pri);
  883.     }
  884.     
  885.     inline void* TOperation::GetCreatorPtr() const
  886.     {
  887.         return fCreatorData.fPtr;
  888.     }
  889.     
  890.     inline void TOperation::SetCreatorPtr(void* data)
  891.     {
  892.         fCreatorData.fPtr = data;
  893.     }
  894.     
  895.     inline long TOperation::GetCreatorData() const
  896.     {
  897.         return fCreatorData.fLong;
  898.     }
  899.     
  900.     inline void TOperation::SetCreatorData(long data)
  901.     {
  902.         fCreatorData.fLong = data;
  903.     }
  904.         
  905.     inline ProcessProc TOperation::GetProcessProc() const
  906.     {
  907.         return fProc;
  908.     }
  909.         
  910.     inline GlobalWorld TOperation::GetSavedGlobalWorld() const
  911.     {
  912.         return fSavedWorld;
  913.     }
  914.         
  915.     inline void TOperation::SetSavedGlobalWorld(GlobalWorld world)
  916.     {
  917.         fSavedWorld = world;
  918.     }
  919.     
  920. /**********************************************************************
  921. ** CLASS TScheduler
  922. **
  923. ** This is the base class for all schedulers.  It allows for scheduling
  924. ** TOperations and removing them from the scheduling queue.
  925. ***********************************************************************/
  926. // %%% Fix a bug in C++
  927.  
  928. #define volatile    
  929.  
  930. #define kTSchedulerID    "!$sked"
  931.  
  932. class TScheduler : public TDynamic
  933. {
  934.     protected:
  935.                             TScheduler();
  936.                             
  937.     public:
  938.         virtual             ~TScheduler();
  939.         
  940.         virtual Boolean        Remove(TOperation*)            = 0;
  941.         virtual TOperation*    Remove(const TMatchObject&)    = 0;
  942.         virtual TOperation*    RemoveNext()                = 0;
  943.         virtual Boolean        IsEmpty() const                = 0;
  944.         
  945.         virtual void        Schedule(TOperation*)        = 0;
  946.         virtual void        Run()                        = 0;
  947.         
  948.                 Boolean        IsSchedulerWorldValid() const;
  949.                 GlobalWorld    GetSchedulerWorld() const;
  950.                 void        SetSchedulerWorld(GlobalWorld);
  951.     protected:
  952.         GlobalWorld            fSchedulerWorld;
  953. };
  954.  
  955. /*    -------------------------------------------------------------------------
  956.     Inline Methods for class TScheduler
  957.     ------------------------------------------------------------------------- */
  958.  
  959.     inline Boolean TScheduler::IsSchedulerWorldValid() const
  960.     {
  961.         return fSchedulerWorld != kInvalidWorld;
  962.     }
  963.  
  964.     inline GlobalWorld TScheduler::GetSchedulerWorld() const
  965.     {
  966.         return fSchedulerWorld;
  967.     }
  968.  
  969.     inline void TScheduler::SetSchedulerWorld(GlobalWorld world)
  970.     {
  971.         fSchedulerWorld = world;
  972.     }
  973.     
  974. /**********************************************************************
  975. ** CLASS TPriorityScheduler
  976. **
  977. ** This class implements a scheduler which simply insures processing
  978. ** of the TOperations by Priority.
  979. ***********************************************************************/
  980.  
  981. #define kTPrioritySchedulerID    "!$prsk"
  982.  
  983. class TPriorityScheduler : public TScheduler
  984. {
  985.     public:
  986.                             TPriorityScheduler();    // autoRun default to false
  987.                             TPriorityScheduler(Boolean ifAutoRun);
  988.         virtual             ~TPriorityScheduler();
  989.         
  990.         virtual Boolean        IsValid() const;
  991.         
  992.             // Returns non-zero if an Operation is removed
  993.         virtual Boolean        Remove(TOperation*);
  994.         virtual TOperation*    Remove(const TMatchObject&);
  995.         virtual TOperation*    RemoveNext();
  996.         virtual Boolean        IsEmpty() const;
  997.         
  998.             // Default implementation processes TOperations on the
  999.             // linked list until it is empty, but insures that 2
  1000.             // threads are not doing it at the same time. In 
  1001.             // Non-AutoRun mode, anything scheduled after "Run"
  1002.             // is called will not Run until "Run" is called again.
  1003.         virtual void        Run();
  1004.         
  1005.             // Default implementation just throws the TOperation on
  1006.             // the linked list.  If 'ifAutoRun' was set, the Run
  1007.             // method is called.  Otherwise, someone must manually
  1008.             // call Run().
  1009.         virtual void        Schedule(TOperation*);
  1010.  
  1011.         virtual void        SetAutoRun(Boolean);
  1012.         
  1013.     protected:
  1014.                 Boolean        IsAutoRun() const;
  1015.                 Boolean        IsSchedulerDead() const;
  1016.                 void        SetSchedulerDead();
  1017.                 
  1018.         TPriorityList    fList;
  1019.         volatile short    fBusy;
  1020.         volatile short    fFlags;
  1021. };
  1022.  
  1023. /*    -------------------------------------------------------------------------
  1024.     Inline methods for TPriorityScheduler
  1025.     ------------------------------------------------------------------------- */
  1026.  
  1027.     inline Boolean TPriorityScheduler::IsAutoRun() const
  1028.     {
  1029.         return fFlags & 1;
  1030.     }
  1031.  
  1032.     inline Boolean TPriorityScheduler::IsSchedulerDead() const
  1033.     {
  1034.         return fFlags < 0;
  1035.     }
  1036.  
  1037.     inline void TPriorityScheduler::SetSchedulerDead()
  1038.     {
  1039.         fFlags = -1;
  1040.     }
  1041.     
  1042. /**********************************************************************
  1043. ** CLASS TSerialScheduler
  1044. **
  1045. ** This class implements a scheduler which simply insures FIFO processing
  1046. ** of the TOperations.  It is the same as a TPriorityScheduler, but 
  1047. ** sets the priority of the TOperation to kNormalPriority before
  1048. ** scheduling.
  1049. ***********************************************************************/
  1050.  
  1051. #define kTSerialSchedulerID    "!$srsk"
  1052.  
  1053. class TSerialScheduler : public TPriorityScheduler
  1054. {
  1055.     public:
  1056.                             TSerialScheduler();    // autoRun default to false
  1057.                             TSerialScheduler(Boolean fIfAutoRun);
  1058.         virtual             ~TSerialScheduler();
  1059.         
  1060.         virtual void        Schedule(TOperation*);
  1061. };
  1062.  
  1063. /**********************************************************************
  1064. ** CLASS TThreadScheduler
  1065. **
  1066. ** This class implements a lightweight 'thread' task.  In the Macintosh
  1067. ** implementation, it is a TPriorityScheduler.
  1068. ***********************************************************************/
  1069.  
  1070. #define kTThreadSchedulerID    "slm:sked$thsk"
  1071.  
  1072. class TThreadScheduler : public TPriorityScheduler
  1073. {
  1074.     public:
  1075.     
  1076.                             TThreadScheduler();
  1077.         virtual             ~TThreadScheduler();
  1078.  
  1079.         virtual void        Schedule(TOperation*);
  1080.         
  1081.     protected:
  1082.         virtual void        Run();
  1083.         
  1084.     private:        
  1085.         Ptr        fData;
  1086. };
  1087.  
  1088. /**********************************************************************
  1089. ** CLASS TTaskScheduler
  1090. **
  1091. ** This class implements a heavy-weight task which can interface to
  1092. ** the operating system.  In the Macintosh, it schedules TOperations to
  1093. ** run at System Task time.
  1094. ***********************************************************************/
  1095.  
  1096. #define kTTaskSchedulerID    "!$task"
  1097.  
  1098. class TTaskScheduler : public TPriorityScheduler
  1099. {
  1100.     public:
  1101.                             TTaskScheduler();
  1102.                             TTaskScheduler(unsigned long pri,
  1103.                                            Boolean runToEmpty = false);
  1104.         virtual             ~TTaskScheduler();
  1105.         
  1106.         virtual void        Schedule(TOperation*);
  1107.         
  1108.         virtual void        SetPriority(unsigned long);
  1109.                 void        SetRunToEmpty(Boolean);
  1110.         
  1111.                 void        ProcessCalled();
  1112.  
  1113.     protected:
  1114.         virtual void        Run();
  1115.  
  1116.     private:
  1117.                 void        InitTaskScheduler(unsigned long);
  1118.         
  1119.     protected:
  1120.         TOperation        fOperation;
  1121.         
  1122.     private:
  1123.         ProcPtr            fSystem;
  1124.         Ptr                fData;
  1125. };
  1126.  
  1127. /*    -------------------------------------------------------------------------
  1128.     Inline methods for TTaskScheduler
  1129.     ------------------------------------------------------------------------- */
  1130.  
  1131.     inline void TTaskScheduler::SetRunToEmpty(Boolean runToEmpty)
  1132.     {
  1133.         SetAutoRun(runToEmpty);
  1134.     }
  1135.     
  1136. /**********************************************************************
  1137. ** CLASS TInterruptScheduler
  1138. **
  1139. ** This class is used by all interrupt service routines to process
  1140. ** incoming data.  All layers above the interrupt service routines
  1141. ** expect to be able to do any processing they want without fear of
  1142. ** interrupts being locked out.  That's what this scheduler insures.
  1143. ***********************************************************************/
  1144.  
  1145. #define kTInterruptSchedulerID    "slm:sked$insk"
  1146.  
  1147. class TInterruptScheduler : public  TPriorityScheduler
  1148. {
  1149.     public:
  1150.     
  1151.                             TInterruptScheduler();
  1152.         virtual             ~TInterruptScheduler();
  1153.         
  1154.         virtual void        Schedule(TOperation*);
  1155.     
  1156.     protected:
  1157.         virtual void        Run();
  1158.         
  1159.     private:
  1160.                 void        Process();
  1161.     
  1162.         Ptr                        fData;
  1163. };
  1164.  
  1165. /**********************************************************************
  1166. ** CLASS TTimeScheduler
  1167. **
  1168. ** This class implements a scheduler which will process TOperations
  1169. ** when a requested amount of time has expired.
  1170. ***********************************************************************/
  1171.  
  1172. const unsigned long kMaxScheduledTime = 0xffffffff;
  1173.  
  1174. #define kTTimeSchedulerID    "slm:sked$tisk"
  1175.  
  1176. class TTimeScheduler : public TScheduler
  1177. {
  1178.     public:
  1179.                                 TTimeScheduler();
  1180.         virtual                 ~TTimeScheduler();
  1181.     
  1182.         virtual Boolean            IsValid() const;
  1183.         
  1184.         virtual Boolean            Remove(TOperation*);
  1185.         virtual TOperation*        Remove(const TMatchObject&);
  1186.         virtual TOperation*        RemoveNext();
  1187.         virtual void            Schedule(TOperation*);
  1188.         virtual Boolean            IsEmpty() const;
  1189.         
  1190.         virtual void            SetResolution(unsigned short msecs);
  1191.         
  1192.     protected:
  1193.         virtual void            Run();
  1194.         virtual unsigned long    ProcessTimerEvent();
  1195.         virtual TOperation*        PrivateRemove(TOperation*, const TMatchObject*);
  1196.         virtual void            ScheduleCheck(TOperation*);
  1197.         virtual Boolean            AboutToSchedule(TOperation*);
  1198.         virtual TOperation*        RemoveCheck(TOperation*, const TMatchObject*);
  1199.         virtual TOperation*        GetNextOperation();
  1200.         virtual void            ProcessOperation(TOperation*);
  1201.                 unsigned long    ProcessTimer();
  1202.                 void            Process();
  1203.  
  1204.     private:    
  1205.         Ptr                    fData;
  1206.         volatile short        fBusy;
  1207.         volatile short        fDead;
  1208.         TPriorityLink*        fList;
  1209.         TSimpleList            fReadyList;
  1210.         unsigned short        fResolution;
  1211.         volatile short        fIfProcessing;
  1212.         
  1213.     protected:
  1214.         volatile long        fSeed;
  1215. };
  1216.  
  1217. #undef volatile
  1218.         
  1219. /*******************************************************************************
  1220. ** CLASS TNotifier
  1221. **
  1222. ** This class and its subclasses are used for asynchronous notification
  1223. ** of events.
  1224. ********************************************************************************/
  1225.  
  1226. #define kTNotifierID "!$noti"
  1227.  
  1228. class TNotifier : public TDynamic
  1229. {
  1230.     public:
  1231.                             TNotifier();
  1232.         virtual                ~TNotifier();
  1233.         
  1234.         virtual void        Notify(EventCode, OSErr = kNoError, 
  1235.                                    void* = NULL) = 0;
  1236.  
  1237.     protected:
  1238.         GlobalWorld    fWorld;    // Global world when constructed.
  1239. };
  1240.  
  1241. /***************************************************************************
  1242. ** CLASS TProcNotifier
  1243. **
  1244. ** This class is the base class for Notifiers that call a "C" procedure
  1245. ** for notification.  If the "refPtr" is left NULL, it will be replaced
  1246. ** with a pointer to the TProcNotifier itself.
  1247. ****************************************************************************/
  1248.  
  1249. #define kTProcNotifierID "!$pnot"
  1250.  
  1251. class TProcNotifier : public TNotifier
  1252. {
  1253.     public:
  1254.                         TProcNotifier(NotifyProc, void* refPtr = NULL);
  1255.                         TProcNotifier(const TProcNotifier&);
  1256.         virtual            ~TProcNotifier();
  1257.         
  1258.         virtual void    Notify(EventCode, OSErr = kNoError, void* = NULL);
  1259.             
  1260.     private:
  1261.         NotifyProc    fProc;
  1262.         void*        fPtr;
  1263. };
  1264.  
  1265. /***************************************************************************
  1266. ** CLASS TMethodNotifier
  1267. **
  1268. ** This class is the base class for Notifiers that call a method in an object.
  1269. ****************************************************************************/
  1270.  
  1271. #define kTMethodNotifierID "!$mnot"
  1272.  
  1273. class TMethodNotifier : public TNotifier
  1274. {
  1275.     public:
  1276.                             TMethodNotifier(TDynamic*, NotifyMethod);
  1277.                             TMethodNotifier(const TMethodNotifier&);
  1278.         virtual                ~TMethodNotifier();
  1279.         
  1280.         virtual void        Notify(EventCode, OSErr = kNoError, void* = NULL);
  1281.  
  1282.                 TDynamic*    GetObject() const;
  1283.         
  1284.     private:
  1285.         TDynamic*        fObject;
  1286.         NotifyMethod    fMethod;
  1287. };
  1288.     
  1289. /*    -----------------------------------------------------------------
  1290.     Inlines for TMethodNotifier
  1291.     ----------------------------------------------------------------- */
  1292.  
  1293.     inline TDynamic* TMethodNotifier::GetObject() const
  1294.     {
  1295.         return fObject;
  1296.     }
  1297.  
  1298. /*******************************************************************************
  1299. ** CLASS TMemoryPool
  1300. **
  1301. ** This is the abstract class from which memory allocators should
  1302. ** descend.
  1303. ********************************************************************************/
  1304.  
  1305. struct PoolInfo
  1306. {
  1307.     size_t    fFreeBytes;
  1308.     size_t    fLargestBlock;
  1309.     size_t    fMaxUsage;
  1310.     size_t    fCurSize;
  1311. };
  1312.  
  1313. #define kTMemoryPoolID "!$pool"
  1314.  
  1315. class TMemoryPool : public TDynamic
  1316. {
  1317.     public:
  1318.         virtual                 ~TMemoryPool();
  1319.         
  1320.                 void*        operator new(size_t, size_t poolSize, 
  1321.                                          ZoneType, MemoryType = kNormalMemory);
  1322.                 void*        operator new(size_t, size_t poolSize, 
  1323.                                          void* zone, MemoryType = kNormalMemory);
  1324.                 void        operator delete(void*);
  1325.         
  1326.         virtual void*            Allocate(size_t)                = 0;
  1327.         virtual void*            Reallocate(void*, size_t)        = 0;
  1328.         virtual void            Free(void*)                        = 0;
  1329.         virtual    size_t            GetSize(void*) const            = 0;
  1330.  
  1331.         virtual Boolean            CheckPool() const                = 0;
  1332.         virtual void            GetPoolInfo(PoolInfo&) const;
  1333.         virtual    void            TracePoolInfo() const;
  1334.  
  1335.         virtual Boolean            AddMemoryToPool(size_t);
  1336.         virtual void            DownsizePool();
  1337.                 size_t            GetCurrentPoolSize() const;
  1338.         
  1339.                 void            SetNotifier(TPoolNotifier*);
  1340.                 TPoolNotifier*    GetNotifier() const;
  1341.                 void            SetNotifyMarks(size_t low, size_t high = (size_t)-1L);
  1342.                 
  1343.         static    TMemoryPool*    RecoverPool(void*);
  1344.         static    void*            AllocateMemory(size_t);
  1345.         static    void*            AllocateMemory(TMemoryPool*, size_t);
  1346.         static    void*            ReallocateMemory(void*, size_t);
  1347.         static    void            FreeMemory(void*);
  1348.         static    size_t            GetMemorySize(void*);
  1349.         
  1350.     protected:
  1351.                                 TMemoryPool();
  1352.         virtual Boolean            PrivateAddMemoryToPool(void*, size_t) = 0;
  1353.         virtual size_t            GetLargestBlockSize() const = 0;
  1354.         virtual Boolean            RemoveBlockFromPool(void*, size_t);
  1355.         
  1356.     private:
  1357.                                 TMemoryPool(const TMemoryPool&);
  1358.                 void            operator=(const TMemoryPool&);
  1359.         
  1360.     private:
  1361.         void*            fMemList;
  1362.         
  1363.     protected:
  1364.         size_t            fSize;
  1365.         size_t            fLowMark;
  1366.         size_t            fHighMark;
  1367.         size_t            fMaxUsed;
  1368.         size_t            fCurFree;
  1369.         void*            fZone;
  1370.         MemoryType        fMemType;
  1371.         short            fFiller;
  1372.         TPoolNotifier*    fNotifier;
  1373.         unsigned long    fSeed;
  1374.         TSemaphore        fSemaphore;
  1375. };
  1376.  
  1377. /*    -----------------------------------------------------------------
  1378.     Inline Methods for TMemoryPool
  1379.     ----------------------------------------------------------------- */
  1380.  
  1381.     inline void TMemoryPool::SetNotifyMarks(size_t low, size_t high)
  1382.     {
  1383.         fLowMark = low;
  1384.         fHighMark= high;
  1385.     }
  1386.     
  1387.     inline TPoolNotifier* TMemoryPool::GetNotifier() const
  1388.     {
  1389.         return fNotifier;
  1390.     }
  1391.  
  1392.     inline void TMemoryPool::SetNotifier(TPoolNotifier* nt)
  1393.     {
  1394.         fNotifier = nt;
  1395.     }
  1396.  
  1397.     inline size_t TMemoryPool::GetCurrentPoolSize() const
  1398.     {
  1399.         return fSize;
  1400.     }
  1401.     
  1402.     inline void* TMemoryPool::AllocateMemory(TMemoryPool* thePool, size_t size)
  1403.     {
  1404.         return thePool->Allocate(size);
  1405.     }
  1406.     
  1407.     inline void* TMemoryPool::AllocateMemory(size_t size)
  1408.     {
  1409.         return ((TMemoryPool*)::GetDefaultPool())->Allocate(size);
  1410.     }
  1411.  
  1412. /*******************************************************************************
  1413. ** CLASS TStandardPool
  1414. **
  1415. ** The general purpose interrupt capable storage allocator.
  1416. ********************************************************************************/
  1417.  
  1418. const size_t kStandardPoolChunkOverhead    = 12;
  1419.  
  1420. #define kTStandardPoolID "!$stdp"
  1421.  
  1422. class TStandardPool : public TMemoryPool
  1423. {
  1424.     public:
  1425.                                 TStandardPool();
  1426.         virtual                 ~TStandardPool();
  1427.  
  1428.         virtual Boolean            IsValid() const;
  1429.  
  1430.         // TMemoryPool Overrides
  1431.         
  1432.         virtual void*            Allocate(size_t);
  1433.         virtual void*            Reallocate(void*, size_t);
  1434.         virtual void            Free(void*);
  1435.         virtual size_t            GetSize(void*) const;
  1436.         virtual Boolean            CheckPool() const;
  1437.     
  1438.     protected:
  1439.         virtual Boolean            PrivateAddMemoryToPool(void*, size_t);
  1440.         virtual size_t            GetLargestBlockSize() const;
  1441.         virtual Boolean            RemoveBlockFromPool(void*, size_t);
  1442.         
  1443.                                 TStandardPool(size_t objSize);
  1444.                                 
  1445.     private:
  1446.                                 TStandardPool(const TStandardPool&);
  1447.                 void            operator=(const TStandardPool&);
  1448.                 
  1449.                 void            InitStandardPool(size_t objSize);
  1450.         
  1451.                 Boolean            FreeChunks(void*, size_t);
  1452.                 void            InternalFree(void*);
  1453.                 
  1454.     protected:    
  1455.         void*            fList;
  1456.     
  1457.     private:
  1458.         void*            fChunks[12];
  1459. };
  1460.     
  1461.  
  1462. /*******************************************************************************
  1463. ** CLASS TChunkyPool
  1464. **
  1465. ** This class implements a Pool where the memory returned is always
  1466. ** the same size (a "chunk").
  1467. ********************************************************************************/
  1468.  
  1469. const size_t kChunkyPoolChunkOverhead = 4;
  1470.  
  1471. #define kTChunkyPoolID "!$chkp"
  1472.  
  1473. class TChunkyPool : public TMemoryPool
  1474. {
  1475.     public:
  1476.                                 TChunkyPool(size_t chunkSize);
  1477.         virtual                    ~TChunkyPool();
  1478.         
  1479.         virtual Boolean            IsValid() const;
  1480.         
  1481.         // TMemoryPool Overrides
  1482.         
  1483.         virtual void*            Allocate(size_t size);
  1484.         virtual void*            Reallocate(void*, size_t);
  1485.         virtual void            Free(void*);
  1486.         virtual size_t            GetSize(void*) const;
  1487.         virtual Boolean            CheckPool() const;
  1488.  
  1489.     protected:        
  1490.         virtual Boolean            PrivateAddMemoryToPool(void*, size_t);
  1491.         virtual size_t            GetLargestBlockSize() const;
  1492.         virtual Boolean            RemoveBlockFromPool(void*, size_t);
  1493.         
  1494.     public:
  1495.         // New methods
  1496.     
  1497.                 size_t            GetChunkSize() const;
  1498.                 size_t            GetNumberOfChunks() const;
  1499.                 
  1500.     private:
  1501.                                 TChunkyPool(const TChunkyPool&);
  1502.                 void            operator=(const TChunkyPool&);
  1503.  
  1504.         size_t            fNumberOfChunks;
  1505.         size_t            fChunkSize;
  1506.         void*            fFreeList;
  1507. };
  1508.  
  1509. /*    -----------------------------------------------------------------
  1510.     Inline Methods for TChunkyPool
  1511.     ----------------------------------------------------------------- */
  1512.     
  1513.     inline size_t TChunkyPool::GetChunkSize() const
  1514.     {
  1515.         return fChunkSize;
  1516.     }
  1517.     
  1518.     inline size_t TChunkyPool::GetNumberOfChunks() const
  1519.     {
  1520.         return fNumberOfChunks;
  1521.     }
  1522.  
  1523. /*******************************************************************************
  1524. ** CLASS TGrowOperation
  1525. **
  1526. ** See TPoolNotifier below
  1527. ********************************************************************************/
  1528.  
  1529. #define kTGrowOperationID    "!$gwop"
  1530.  
  1531. class TGrowOperation : public TOperation
  1532. {
  1533.     public:
  1534.                         TGrowOperation();
  1535.         virtual            ~TGrowOperation();
  1536.         
  1537.         virtual    void    Process();
  1538.  
  1539.         size_t        fGrowBy;
  1540.         Boolean*    fOpInUse;
  1541. };
  1542.  
  1543. /*******************************************************************************
  1544. ** CLASS TPoolNotifier
  1545. **
  1546. ** Used by TMemoryPools so they can be notified when the pool reaches a low or
  1547. ** high water mark. The TGrowOperation is not processed at interrupt time so it
  1548. ** is always safe for it to grow the pool.
  1549. ********************************************************************************/
  1550.  
  1551. #define kTPoolNotifierID    "!$plnt"
  1552.  
  1553. class TPoolNotifier : public TNotifier
  1554. {
  1555.     public:
  1556.                         TPoolNotifier(unsigned short growBy = 10, 
  1557.                                       unsigned short minGrow = 128);
  1558.         virtual            ~TPoolNotifier();
  1559.  
  1560.         virtual void    Notify(EventCode, OSErr = kNoError, void* = NULL);
  1561.         virtual size_t    GrowBy(TMemoryPool*, size_t);
  1562.         
  1563.     private:
  1564.                 void    InitNotifier(unsigned short, unsigned short);
  1565.     
  1566.         TGrowOperation    fOperation;
  1567.         unsigned short    fMinSize;
  1568.         unsigned short    fGrowBy;
  1569.         Boolean            fOpInUse;
  1570. };
  1571.  
  1572. /**********************************************************************
  1573. ** CLASS TArbitrator
  1574. ***********************************************************************/
  1575.  
  1576. #define kTArbitratorID    "!$arbt"
  1577.  
  1578. #define kRequestIDPrefix '?'
  1579. #define kRequestIDPrefixSize 1
  1580.  
  1581. enum TokenRequestType
  1582. {
  1583.     kInvalidTokenRequest, kRequestTokenRequest, 
  1584.     kExclusiveTokenRequest, kSharedTokenRequest
  1585. };
  1586.     
  1587. class TArbitrator : public THashObject
  1588. {
  1589.     friend class    TToken;
  1590.     public:
  1591.                                     TArbitrator(TStandardPool* = NULL,
  1592.                                                 size_t defSize = 0);
  1593.         virtual                        ~TArbitrator();
  1594.         
  1595.         virtual OSErr                RegisterObject(char* theID, void* theObject);
  1596.         virtual    void*                UnregisterObject(char* theID);
  1597.         virtual void*                LookupObject(char* theID);
  1598.  
  1599.         virtual OSErr                RegisterToken(TToken*);
  1600.         virtual TToken*                GetToken(char* theID, TokenRequestType);
  1601.  
  1602.         virtual TRequestToken*        PassiveRequest(char* theID, TokenRequestType, TNotifier* = NULL, Boolean registerIfFirst = false);
  1603.         virtual TRequestToken*        ActiveRequest(char* theID, TokenRequestType, TNotifier* = NULL, Boolean registerIfFirst = false);
  1604.         virtual TRequestToken*         GetRequest(char* theID);
  1605.  
  1606.         virtual Boolean                NotifyOwners(TRequestToken* theRequest);
  1607.         virtual unsigned long        Hash(const void*) const;
  1608.  
  1609.     private: // methods
  1610.         virtual void                ReleaseToken(TToken* theToken);
  1611.         virtual TToken*                NewToken(char* theID, void* = NULL);
  1612.         virtual TRequestToken*         NewRequestToken(char* theID, TokenRequestType, TNotifier* = NULL);
  1613.         virtual void                UnregisterToken(TToken*);
  1614.  
  1615.     private: // data
  1616.         TCollection*    fHashList;
  1617. };
  1618.  
  1619. /**********************************************************************
  1620. ** Class TTokenNotification (inline methods only)
  1621. ***********************************************************************/
  1622.  
  1623. class TTokenNotification
  1624. {
  1625.     public:
  1626.                             TTokenNotification(TToken*, TRequestToken*);
  1627.                             ~TTokenNotification();
  1628.         TToken*                GetToken();
  1629.         TRequestToken*        GetRequestToken();
  1630.     private:
  1631.         TToken*                fToken;
  1632.         TRequestToken*        fRequestToken;
  1633. };
  1634.  
  1635. /*    -----------------------------------------------------------------
  1636.     inline methods for TTokenNotification
  1637.     ----------------------------------------------------------------- */
  1638.     
  1639.     inline TToken* TTokenNotification::GetToken()
  1640.     {
  1641.         return fToken;
  1642.     }
  1643.     
  1644.     inline TRequestToken* TTokenNotification::GetRequestToken()
  1645.     {
  1646.         return fRequestToken;
  1647.     }
  1648.     
  1649.     
  1650. /**********************************************************************
  1651. ** CLASS TToken
  1652. ***********************************************************************/
  1653.  
  1654. #define kTTokenID    "!$tokn"
  1655.  
  1656. class TToken : public TMatchObject
  1657. {
  1658.     friend class    TArbitrator;
  1659.  
  1660.     public:
  1661.         virtual                        ~TToken();
  1662.  
  1663.         virtual Boolean                Get(TokenRequestType);
  1664.         virtual void                Release();
  1665.         virtual Boolean                Request(TRequestToken*);
  1666.         virtual Boolean                Notify(TRequestToken*);
  1667.         virtual TokenRequestType    GetRequestType() const;
  1668.  
  1669.                 char*                GetID() const;
  1670.  
  1671.                 void*                GetObject() const;
  1672.                 void                SetObject(void* theObject);
  1673.  
  1674.                 TNotifier*            GetNotifier() const;
  1675.                 void                SetNotifier(TNotifier*);
  1676.  
  1677.         virtual Boolean                IsEqual(const void*) const;
  1678.         virtual unsigned long        Hash() const;
  1679.         
  1680.                 long                GetUseCount() const;
  1681.  
  1682.     protected:
  1683.                                     TToken();
  1684.  
  1685.     private:
  1686.         virtual void                ReallyRelease();
  1687.  
  1688.     protected:
  1689.         TSemaphore                    fSemaphore;
  1690.         char*                        fID;
  1691.         void*                        fObject;
  1692.         TArbitrator*                fArbitrator;
  1693.         long                        fUseCount;
  1694.         TNotifier*                    fNotifier;
  1695. };
  1696.  
  1697. /*    -----------------------------------------------------------------
  1698.     inlines for TToken
  1699.     ----------------------------------------------------------------- */
  1700.     
  1701.     inline char* TToken::GetID() const
  1702.     {
  1703.         return fID;
  1704.     }
  1705.     
  1706.     inline void* TToken::GetObject() const
  1707.     {
  1708.         return fObject;
  1709.     }
  1710.     
  1711.     inline void TToken::SetObject(void* theObject)
  1712.     {
  1713.         fObject = theObject;
  1714.     }
  1715.     
  1716.     inline TNotifier* TToken::GetNotifier() const
  1717.     {
  1718.         return fNotifier;
  1719.     }
  1720.     
  1721.     inline void TToken::SetNotifier(TNotifier* theNotifier)
  1722.     {
  1723.         fNotifier = theNotifier;
  1724.     }
  1725.     
  1726.     inline long TToken::GetUseCount() const
  1727.     {
  1728.         return fUseCount;
  1729.     }
  1730.  
  1731. /**********************************************************************
  1732. ** CLASS TRequestToken 
  1733. ***********************************************************************/
  1734.  
  1735. #define kTRequestTokenID    "!$rqtk"
  1736.  
  1737. class TRequestToken : public TToken
  1738. {
  1739.     friend class     TArbitrator;
  1740.  
  1741.     public:
  1742.         virtual                        ~TRequestToken();
  1743.         
  1744.         virtual Boolean                IsEqual(const void*) const;
  1745.         virtual unsigned long        Hash() const;
  1746.  
  1747.         virtual Boolean                Give(TToken* theToken);
  1748.         virtual TToken*                Exchange();
  1749.         virtual void                RequestAgain();
  1750.  
  1751.         virtual    TokenRequestType    GetRequestType() const;
  1752.         virtual    void                SetRequestType(TokenRequestType);
  1753.  
  1754.                 Boolean                IsTokenRegistered() const;
  1755.  
  1756.     private:
  1757.                                     TRequestToken();
  1758.  
  1759.     private: // data
  1760.         Boolean                        fTokenRegistered;
  1761.         Boolean                        fActive;
  1762.         TokenRequestType            fRequestType;
  1763. };
  1764.  
  1765. /*    -----------------------------------------------------------------
  1766.     inlines for TRequestToken
  1767.     ----------------------------------------------------------------- */
  1768.  
  1769.     inline Boolean TRequestToken::IsTokenRegistered() const
  1770.     {
  1771.         return fTokenRegistered;
  1772.     }
  1773.     
  1774.     
  1775. /*******************************************************************************
  1776. ** class TClassInfo
  1777. ********************************************************************************/
  1778.  
  1779. #define kTClassInfoID "slm:supp$clif"
  1780.  
  1781. class TClassInfo : public TIterator
  1782. {
  1783.     friend class TLibraryManager;
  1784.  
  1785.     public:
  1786.         virtual                 ~TClassInfo();
  1787.  
  1788.     // TIterator overrides
  1789.         virtual    void            Reset();
  1790.         virtual void*            Next();    // safe to cast to TClassID* or char*
  1791.  
  1792.         virtual Boolean            IterationComplete() const;
  1793.         virtual Boolean            RemoveCurrentObject();    // do nothing instead
  1794.  
  1795.     // TClassInfo methods
  1796.                 void            SetBaseClassID(const TClassID& classID);
  1797.  
  1798.                 TClassID*        GetClassID();
  1799.                 TClassID*        GetClassParentID();
  1800.                 TConfiguration*    GetClassConfiguration() const;
  1801.  
  1802.                 Boolean            GetClassNewObjectFlag() const;
  1803.                 Boolean            GetClassPreloadFlag() const;
  1804.                 size_t            GetClassSize() const;
  1805.  
  1806.     private:
  1807.                                 TClassInfo();
  1808.  
  1809.     private:
  1810.         TClassID                fBaseClassID;
  1811.         TClassID                fClassID;
  1812.         TClassID                fParentID;
  1813.         TConfiguration*            fConfiguration;
  1814.         Boolean                    fNewObjectFlag;
  1815.         Boolean                    fPreloadFlag;
  1816.         size_t                    fSize;
  1817. };
  1818.  
  1819. /*    -------------------------------------------------------------------------
  1820.     inline methods of TClassInfo
  1821.     ------------------------------------------------------------------------- */
  1822.     
  1823.     inline void TClassInfo::SetBaseClassID(const TClassID& classID)
  1824.     {
  1825.         fBaseClassID = classID;
  1826.         Reset();
  1827.     }
  1828.     
  1829.     inline TClassID* TClassInfo::GetClassID()
  1830.     {
  1831.         return &fClassID;
  1832.     }
  1833.     
  1834.     inline TClassID* TClassInfo::GetClassParentID()
  1835.     {
  1836.         return &fParentID;
  1837.     }
  1838.     
  1839.     inline TConfiguration* TClassInfo::GetClassConfiguration() const
  1840.     {
  1841.         return fConfiguration;
  1842.     }
  1843.     
  1844.  
  1845.     inline Boolean TClassInfo::GetClassNewObjectFlag() const
  1846.     {
  1847.         return fNewObjectFlag;
  1848.     }
  1849.     
  1850.     inline Boolean TClassInfo::GetClassPreloadFlag() const
  1851.     {
  1852.         return fPreloadFlag;
  1853.     }
  1854.     
  1855.     inline size_t TClassInfo::GetClassSize() const
  1856.     {
  1857.         return fSize;
  1858.     }
  1859.     
  1860. /**********************************************************************
  1861. ** Class TLibraryFile
  1862. **
  1863. ** Used mainly to get resources out of a library. A C interface is also
  1864. ** provided in TLibraryManagerUtilities.h
  1865. ***********************************************************************/
  1866.  
  1867. #define kTLibraryFileID "!$lfil"
  1868.  
  1869. extern "C" TLibraryFile* GetLocalLibraryFile();
  1870.     
  1871. class TLibraryFile : public TDynamic 
  1872. {
  1873.     public:
  1874.         virtual                 ~TLibraryFile();
  1875.  
  1876.         // New Methods
  1877.         
  1878.         virtual    OSErr            Preflight(short& savedRefNum) = 0;
  1879.         virtual    OSErr            Postflight(short savedRefNum) = 0;
  1880.         
  1881.         virtual    Handle            GetSharedResource(ResType theType, short theID) = 0;
  1882.         virtual    Handle            GetSharedIndResource(ResType theType, short index) = 0;
  1883.         virtual    Handle            GetSharedNamedResource(ResType theType, ConstStr255Param name) = 0;
  1884.         
  1885.         virtual    void            ReleaseSharedResource(Handle) = 0;
  1886.         virtual long            CountSharedResources(ResType theType) = 0;
  1887.         
  1888.         virtual    size_t            GetSharedResourceUseCount(Handle) const = 0;
  1889.         
  1890.         virtual    OSErr            CloseLibrary() = 0;
  1891.         virtual long            GetRefNum() const = 0;
  1892.         virtual    long            GetFileID() const = 0;
  1893.         virtual    short            GetVolumeRefNum() const = 0;
  1894.     
  1895.     protected:
  1896.                                 TLibraryFile();
  1897. };
  1898.  
  1899. /**********************************************************************
  1900. ** CLASS TBitmap
  1901. ***********************************************************************/
  1902.  
  1903. #define kTBitmapID "slm:supp$bmap"
  1904.  
  1905. class TBitmap : public TDynamic
  1906. {
  1907.     public:
  1908.                                 TBitmap(size_t numBits, TMemoryPool* pool);
  1909.                                 TBitmap(Ptr bits, size_t nBits);
  1910.         virtual                    ~TBitmap();
  1911.                             
  1912.         
  1913.         // NOTE: There is no ErrorChecking on these first 3 calls.
  1914.         
  1915.         virtual    Boolean            SetBit(size_t);
  1916.         virtual    Boolean            ClearBit(size_t);
  1917.         virtual    Boolean            TestBit(size_t);
  1918.                 
  1919.                 // These return -1 if no free bits
  1920.                 
  1921.         virtual    long            SetFirstClearBit();                
  1922.         virtual    long            SetFirstClearBit(size_t, size_t);
  1923.     
  1924.     private:
  1925.                 void            InitBitmap(size_t numBits, TMemoryPool* pool);
  1926.                 void            InitBitmap(Ptr bits, size_t nBits);
  1927.         
  1928.         unsigned char*            fBits;
  1929.         size_t                    fNumberBits;
  1930.         Boolean                    fIsNewd;
  1931. };
  1932.  
  1933. /**********************************************************************
  1934. ** CLASS TDoubleLong
  1935. **
  1936. ** Implements a TDynamic double long (64 bits).  Normally this is used
  1937. ** as a superclass for some other class which has a 64-bit value as its 
  1938. ** comparable/hashing value  (the default hash value is the low 32-bits).
  1939. ***********************************************************************/
  1940.  
  1941. #define kTDoubleLongID "slm:supp$dbll"
  1942.  
  1943. class TDoubleLong : public TMatchObject
  1944. {
  1945.     public:
  1946.                                 TDoubleLong(const TDoubleLong&);
  1947.                                 TDoubleLong(unsigned long low, long hi);
  1948.                                 TDoubleLong(long l);
  1949.                                 TDoubleLong();
  1950.         virtual                    ~TDoubleLong();
  1951.     
  1952.         virtual    OSErr            Inflate(TFormattedStream&);
  1953.         virtual    OSErr            Flatten(TFormattedStream&) const;
  1954.         
  1955.         virtual Boolean            IsEqual(const void*) const;
  1956.         virtual unsigned long    Hash() const;
  1957.         
  1958.         virtual                 operator double() const;
  1959.                                 operator unsigned long() const;
  1960.                                 
  1961.         virtual    TDoubleLong&    Add(const TDoubleLong&);
  1962.         virtual    TDoubleLong&    Subtract(const TDoubleLong&);
  1963.         virtual    TDoubleLong&    Multiply(const TDoubleLong&);
  1964.         virtual    TDoubleLong&    Divide(const TDoubleLong&);
  1965.         virtual    TDoubleLong&    Modulo(const TDoubleLong&);
  1966.         virtual    TDoubleLong        RShift(unsigned short) const;
  1967.         virtual    TDoubleLong        LShift(unsigned short) const;
  1968.         virtual TDoubleLong&    Negate();
  1969.         virtual short            Compare(const void*) const;
  1970.         
  1971.                 TDoubleLong&    operator=(const TDoubleLong&);
  1972.                 TDoubleLong&    operator+=(const TDoubleLong&);
  1973.                 TDoubleLong&    operator-=(const TDoubleLong&);
  1974.                 TDoubleLong&    operator*=(const TDoubleLong&);
  1975.                 TDoubleLong&    operator/=(const TDoubleLong&);
  1976.                 TDoubleLong&    operator%=(const TDoubleLong&);
  1977.                 TDoubleLong&    operator&=(const TDoubleLong&);
  1978.                 TDoubleLong&    operator|=(const TDoubleLong&);
  1979.                 TDoubleLong&    operator^=(const TDoubleLong&);
  1980.                 TDoubleLong&    operator~();
  1981.                 TDoubleLong&    operator-();
  1982.                 
  1983.                 TDoubleLong        operator+(const TDoubleLong&) const;
  1984.                 TDoubleLong        operator-(const TDoubleLong&) const;
  1985.                 TDoubleLong        operator*(const TDoubleLong&) const;
  1986.                 TDoubleLong        operator/(const TDoubleLong&) const;
  1987.                 TDoubleLong        operator%(const TDoubleLong&) const;
  1988.                 TDoubleLong        operator&(const TDoubleLong&) const;
  1989.                 TDoubleLong        operator|(const TDoubleLong&) const;
  1990.                 TDoubleLong        operator^(const TDoubleLong&) const;
  1991.                 
  1992.                 TDoubleLong        operator<<(unsigned short) const;
  1993.                 TDoubleLong        operator>>(unsigned short) const;
  1994.     
  1995.                 Boolean            operator>(const TDoubleLong&) const;
  1996.                 Boolean            operator<(const TDoubleLong&) const;
  1997.                 Boolean            operator<=(const TDoubleLong&) const;
  1998.                 Boolean            operator>=(const TDoubleLong&) const;
  1999.                 Boolean            operator==(const TDoubleLong&) const;
  2000.                 Boolean            operator!=(const TDoubleLong&) const;
  2001.                                 
  2002.     protected:
  2003.         long            fHiBits;
  2004.         unsigned long    fLoBits;
  2005. };
  2006.  
  2007. /*    -----------------------------------------------------------------
  2008.     INLINE Methods for TDoubleLong
  2009.     ----------------------------------------------------------------- */
  2010.     
  2011.     inline TDoubleLong::operator unsigned long() const
  2012.     {
  2013.         return fLoBits;
  2014.     }
  2015.     
  2016.     inline TDoubleLong TDoubleLong::operator &(const TDoubleLong& val) const
  2017.     {
  2018.         return TDoubleLong(fLoBits & val.fLoBits, fHiBits & val.fHiBits);
  2019.     }
  2020.     
  2021.     inline TDoubleLong& TDoubleLong::operator =(const TDoubleLong& val)
  2022.     {
  2023.         fHiBits = val.fHiBits;
  2024.         fLoBits = val.fLoBits;
  2025.         return *this;
  2026.     }
  2027.     
  2028.     inline TDoubleLong& TDoubleLong::operator +=(const TDoubleLong& val)
  2029.     {
  2030.         return Add(val);
  2031.     }
  2032.     
  2033.     inline TDoubleLong& TDoubleLong::operator -=(const TDoubleLong& val)
  2034.     {
  2035.         return Subtract(val);
  2036.     }
  2037.     
  2038.     inline TDoubleLong& TDoubleLong::operator *=(const TDoubleLong& val)
  2039.     {
  2040.         return Multiply(val);
  2041.     }
  2042.     
  2043.     inline TDoubleLong& TDoubleLong::operator /=(const TDoubleLong& val)
  2044.     {
  2045.         return Divide(val);
  2046.     }
  2047.             
  2048.     inline TDoubleLong& TDoubleLong::operator %=(const TDoubleLong& val)
  2049.     {
  2050.         return Modulo(val);
  2051.     }
  2052.  
  2053.     inline TDoubleLong& TDoubleLong::operator &=(const TDoubleLong& val)
  2054.     {
  2055.         fHiBits &= val.fHiBits;
  2056.         fLoBits &= val.fLoBits;
  2057.         return *this;
  2058.     }
  2059.     
  2060.     inline TDoubleLong& TDoubleLong::operator |=(const TDoubleLong& val)
  2061.     {
  2062.         fHiBits |= val.fHiBits;
  2063.         fLoBits |= val.fLoBits;
  2064.         return *this;
  2065.     }
  2066.     
  2067.     inline TDoubleLong& TDoubleLong::operator ^=(const TDoubleLong& val)
  2068.     {
  2069.         fHiBits ^= val.fHiBits;
  2070.         fLoBits ^= val.fLoBits;
  2071.         return *this;
  2072.     }
  2073.     
  2074.     inline TDoubleLong TDoubleLong::operator +(const TDoubleLong& val) const
  2075.     {
  2076.         TDoubleLong    temp(*this);
  2077.         return (&temp)->Add(val);
  2078.     }
  2079.     
  2080.     inline TDoubleLong TDoubleLong::operator -(const TDoubleLong& val) const
  2081.     {
  2082.         TDoubleLong    temp(*this);
  2083.         return (&temp)->Subtract(val);
  2084.     }
  2085.     
  2086.     inline TDoubleLong TDoubleLong::operator *(const TDoubleLong& val) const
  2087.     {
  2088.         TDoubleLong    temp(*this);
  2089.         return (&temp)->Multiply(val);
  2090.     }
  2091.     
  2092.     inline TDoubleLong TDoubleLong::operator /(const TDoubleLong& val) const
  2093.     {
  2094.         TDoubleLong    temp(*this);
  2095.         return (&temp)->Divide(val);
  2096.     }
  2097.     
  2098.     inline TDoubleLong TDoubleLong::operator %(const TDoubleLong& val) const
  2099.     {
  2100.         TDoubleLong    temp(*this);
  2101.         return (&temp)->Modulo(val);
  2102.     }
  2103.     
  2104.     inline TDoubleLong TDoubleLong::operator |(const TDoubleLong& val) const
  2105.     {
  2106.         return TDoubleLong(fLoBits | val.fLoBits, fHiBits | val.fHiBits);
  2107.     }
  2108.     
  2109.     inline TDoubleLong TDoubleLong::operator ^(const TDoubleLong& val) const
  2110.     {
  2111.         return TDoubleLong(fLoBits ^ val.fLoBits, fHiBits ^ val.fHiBits);
  2112.     }
  2113.     
  2114.     inline TDoubleLong& TDoubleLong::operator~()
  2115.     {
  2116.         fHiBits = ~fHiBits;
  2117.         fLoBits = ~fLoBits;
  2118.         return *this;
  2119.     }
  2120.     
  2121.     inline TDoubleLong& TDoubleLong::operator -()
  2122.     {
  2123.         return Negate();
  2124.     }
  2125.     
  2126.     inline TDoubleLong TDoubleLong::operator >>(unsigned short val) const
  2127.     {
  2128.         return RShift(val);
  2129.     }
  2130.     
  2131.     inline TDoubleLong TDoubleLong::operator <<(unsigned short val) const
  2132.     {
  2133.         return LShift(val);
  2134.     }
  2135.     
  2136.     inline Boolean TDoubleLong::operator ==(const TDoubleLong& val) const
  2137.     {
  2138.         return fLoBits == val.fLoBits && fHiBits == val.fHiBits;
  2139.     }
  2140.     
  2141.     inline Boolean TDoubleLong::operator !=(const TDoubleLong& val) const
  2142.     {
  2143.         return fLoBits != val.fLoBits || fHiBits != val.fHiBits;
  2144.     }
  2145.  
  2146.     inline Boolean TDoubleLong::operator >(const TDoubleLong& val) const
  2147.     {
  2148.         return Compare(&val) > 0;
  2149.     }
  2150.     
  2151.     inline Boolean TDoubleLong::operator >=(const TDoubleLong& val) const
  2152.     {
  2153.         return Compare(&val) >= 0;
  2154.     }
  2155.     
  2156.     inline Boolean TDoubleLong::operator <(const TDoubleLong& val) const
  2157.     {
  2158.         return Compare(&val) < 0;
  2159.     }
  2160.     
  2161.     inline Boolean TDoubleLong::operator <=(const TDoubleLong& val) const
  2162.     {
  2163.         return Compare(&val) <= 0;
  2164.     }
  2165.     
  2166. /**********************************************************************
  2167. ** CLASS THashDoubleLong
  2168. **
  2169. ** Class to compare a TDoubleLong.  The 'const Ptr' parameter is a 
  2170. ** pointer to a TDoubleLong object.
  2171. ***********************************************************************/
  2172.  
  2173. #define kTHashDoubleLongID "slm:supp$hdbl"
  2174.  
  2175. class THashDoubleLong : public THashObject
  2176. {
  2177.     public:
  2178.                                 THashDoubleLong();
  2179.         virtual                    ~THashDoubleLong();
  2180.         
  2181.         virtual unsigned long    Hash(const void*) const;
  2182. };
  2183.  
  2184. /**********************************************************************
  2185. ** CLASS TTime
  2186. **
  2187. ** This is the superclass for all Time-related classes.  Internally,
  2188. ** all times are stored as microSeconds, and the casting operators
  2189. ** for the TTime class return values converted to microseconds.
  2190. ***********************************************************************/
  2191.  
  2192. #define kTTimeID "slm:supp$time"
  2193.  
  2194. class TTime : public TDoubleLong
  2195. {
  2196.     public:
  2197.                                 TTime();
  2198.                                 TTime(unsigned long microseconds);
  2199.                                 TTime(const TDoubleLong&);
  2200.                                 TTime(const TTime&);
  2201.         virtual                    ~TTime();
  2202.         
  2203.                 void            SetTime(const TTime&);
  2204.                 
  2205.                 void            SetMicroseconds(unsigned long);
  2206.         virtual    void            SetMilliseconds(unsigned long);
  2207.         virtual    void            SetSeconds(unsigned long);
  2208.                 
  2209.                 unsigned long    GetMicroseconds() const;
  2210.         virtual    unsigned long    GetMilliseconds() const;
  2211.         virtual    unsigned long    GetSeconds() const;
  2212. };
  2213.  
  2214. /*    -----------------------------------------------------------------
  2215.     INLINE Methods for TTime
  2216.     ----------------------------------------------------------------- */
  2217.  
  2218.     inline void TTime::SetTime(const TTime& time)
  2219.     {
  2220.         fLoBits    = time.fLoBits;
  2221.         fHiBits    = time.fHiBits;
  2222.     }
  2223.     
  2224.     inline void TTime::SetMicroseconds(unsigned long val)
  2225.     {
  2226.         fLoBits    = val;
  2227.         fHiBits    = 0;
  2228.     }
  2229.     
  2230.     inline unsigned long TTime::GetMicroseconds() const
  2231.     {
  2232.         return fLoBits;
  2233.     }
  2234.  
  2235. /**********************************************************************
  2236. ** CLASS TMicroseconds
  2237. ***********************************************************************/
  2238.  
  2239. #define kTMicrosecondsID "slm:supp$mics"
  2240.  
  2241. class TMicroseconds : public TTime
  2242. {
  2243.     public:
  2244.                                 TMicroseconds();
  2245.                                 TMicroseconds(unsigned long msecs);
  2246.                                 ~TMicroseconds();
  2247.  
  2248.                 operator        unsigned long() const;
  2249.         virtual    operator        double() const;
  2250.         
  2251.     private:
  2252.                                 TMicroseconds(const TMicroseconds&);
  2253.                 void            operator=(const TMicroseconds&);
  2254. };
  2255.  
  2256. /*    -----------------------------------------------------------------
  2257.     INLINE Methods for TMicroseconds
  2258.     ----------------------------------------------------------------- */
  2259.     
  2260.     inline TMicroseconds::operator unsigned long() const
  2261.     {
  2262.         return GetMicroseconds();
  2263.     }
  2264.  
  2265. /**********************************************************************
  2266. ** CLASS TMilliseconds
  2267. ***********************************************************************/
  2268.  
  2269. #define kTMillisecondsID "slm:supp$mils"
  2270.  
  2271. class TMilliseconds : public TTime
  2272. {
  2273.     public:
  2274.                                 TMilliseconds();
  2275.                                 TMilliseconds(unsigned long msecs);
  2276.                                 ~TMilliseconds();
  2277.  
  2278.                 operator        unsigned long() const;
  2279.         virtual    operator        double() const;
  2280.         
  2281.     private:
  2282.                                 TMilliseconds(const TMilliseconds&);
  2283.                 void            operator=(const TMilliseconds&);
  2284. };
  2285.  
  2286. /*    -----------------------------------------------------------------
  2287.     INLINE Methods for TMilliseconds
  2288.     ----------------------------------------------------------------- */
  2289.     
  2290.     inline TMilliseconds::operator unsigned long() const
  2291.     {
  2292.         return GetMilliseconds();
  2293.     }
  2294.     
  2295. /**********************************************************************
  2296. ** CLASS TSeconds
  2297. ***********************************************************************/
  2298.  
  2299. #define kTSecondsID "slm:supp$secs"
  2300.  
  2301. class TSeconds : public TTime
  2302. {
  2303.     public:
  2304.                                 TSeconds();
  2305.                                 TSeconds(unsigned long secs);
  2306.                                 ~TSeconds();
  2307.  
  2308.                 operator        unsigned long() const;
  2309.         virtual    operator        double() const;
  2310.         
  2311.     private:
  2312.                                 TSeconds(const TSeconds&);
  2313.                 void            operator=(const TSeconds&);
  2314. };
  2315.  
  2316. /*    -----------------------------------------------------------------
  2317.     INLINE Methods for TSeconds
  2318.     ----------------------------------------------------------------- */
  2319.  
  2320.     inline TSeconds::operator unsigned long() const
  2321.     {
  2322.         return GetSeconds();
  2323.     }
  2324.     
  2325. /**********************************************************************
  2326. ** CLASS TTimeStamp
  2327. ***********************************************************************/
  2328.  
  2329. #define kTTimeStampID "slm:supp$tstm"
  2330.  
  2331. class TTimeStamp : public TTime
  2332. {
  2333.     public:
  2334.                             TTimeStamp();
  2335.         virtual                ~TTimeStamp();
  2336.     
  2337.         virtual    void        SetTimeStamp();
  2338.         
  2339.     private:
  2340.                                 TTimeStamp(const TTimeStamp&);
  2341.                 void            operator=(const TTimeStamp&);
  2342. };
  2343.  
  2344. /**********************************************************************
  2345. ** CLASS TStopwatch
  2346. ***********************************************************************/
  2347.  
  2348. #define kTStopwatchID "slm:supp$stpw"
  2349.  
  2350. class TStopwatch : public TTimeStamp
  2351. {
  2352.     public:
  2353.                                 TStopwatch();
  2354.         virtual                    ~TStopwatch();
  2355.     
  2356.         virtual    void            Reset();
  2357.  
  2358.         virtual    unsigned long    ElapsedMicroseconds() const;
  2359.         virtual    unsigned long    ElapsedMilliseconds() const;
  2360.         virtual    unsigned long    ElapsedSeconds() const;
  2361.         
  2362.     private:
  2363.                                 TStopwatch(const TStopwatch&);
  2364.                 void            operator=(const TStopwatch&);
  2365. };
  2366.     
  2367. /*******************************************************************************
  2368. ** CLASS TTraceLog
  2369. **
  2370. ** An object for doing debug tracing with.
  2371. ********************************************************************************/
  2372.  
  2373. #define kTTraceLogID "slm:dbug$tlog"
  2374.  
  2375. class TTraceLog : public TDynamic 
  2376. {
  2377.     public:
  2378.                                 TTraceLog();
  2379.         virtual                    ~TTraceLog();
  2380.     
  2381.         virtual void            Trace(char *formatStr, ...) const;
  2382.         
  2383.     // New methods
  2384.         
  2385.                 Boolean            IsTraceLogOn() const;
  2386.                 void            TraceLogOn();
  2387.                 void            TraceLogOff();
  2388.                 
  2389.         virtual    void            TraceFormatted(char* outstr) = 0;
  2390.         virtual    void            TraceUnformatted(void* argp);
  2391.  
  2392.     protected:
  2393.                 void            SetTracePool(TStandardPool*);
  2394.                 TStandardPool*    GetTracePool() const;
  2395.  
  2396.     private:
  2397.                                 TTraceLog(const TTraceLog&);
  2398.                 void            operator=(const TTraceLog&);
  2399.         
  2400.         Boolean                    fTracing;
  2401.         TStandardPool*            fTracePool;
  2402. };
  2403.  
  2404. /*    -----------------------------------------------------------------
  2405.     Inline Methods for TTraceLog
  2406.     ----------------------------------------------------------------- */
  2407.  
  2408.     inline Boolean TTraceLog::IsTraceLogOn() const
  2409.     {
  2410.         return fTracing;
  2411.     }
  2412.     
  2413.     inline void TTraceLog::TraceLogOn()
  2414.     {
  2415.         if (fTracePool != NULL)
  2416.             fTracing = true;
  2417.     }
  2418.     
  2419.     inline void TTraceLog::TraceLogOff()
  2420.     {
  2421.         fTracing = false;
  2422.     }
  2423.     
  2424.     inline void TTraceLog::SetTracePool(TStandardPool* pool)
  2425.     {
  2426.         fTracePool = pool;
  2427.     }
  2428.     
  2429.     inline TStandardPool* TTraceLog::GetTracePool() const
  2430.     {
  2431.         return fTracePool;
  2432.     }
  2433.  
  2434. /*******************************************************************************
  2435. ** EXCEPTION Handling
  2436. **
  2437. ** Some RULES:
  2438. ** 1) Never propogate a failure outside of a constructor or destructor.
  2439. **    If your constructor or destructor can call something which fails, it
  2440. **    _must_ EXCEPT the failure and not re-propogate it.
  2441. ** 2) Never create an object inside of a "try" block which you cannot 
  2442. **    destroy (especially an auto object).
  2443. ** 3) Falling through the end of a "EXCEPT" will re-throw the exception.
  2444. **    You must "break" out of a EXCEPT in order not to re-throw it.
  2445. ** 4) if you are going to just "fall" out of the "EXCEPT" to re-throw
  2446. **    the exception, you must manually call the destructors of any 
  2447. **    auto objects that are still in scope, before falling out!
  2448. ** 5) Any variables that are changed inside the "try", and which are tested
  2449. **    inside the "EXCEPT" must be declared "volatile" (Use the VOLATILE macro
  2450. **    below until C++ and volatile work!)
  2451. ** 6) Never call Failure while an auto variable is in scope - it's 
  2452. **      destructor will not be called unless you call it manually.
  2453. ********************************************************************************/
  2454.  
  2455. #ifndef __SETJMP__
  2456. #include <SetJmp.h>
  2457. #endif
  2458.  
  2459. struct TException
  2460. {
  2461.     static void            Push(TException*);
  2462.     static TException*    Pop();
  2463.     
  2464.     TException*            fPrev;
  2465.     jmp_buf                fBuffer;
  2466.     char*                fMessage;
  2467.     void*                fReserved;
  2468.     OSErr                fError;
  2469. };
  2470.  
  2471.     
  2472. /*    -----------------------------------------------------------------
  2473.     Some important functions for exception handling
  2474.     ----------------------------------------------------------------- */
  2475.  
  2476. void Failure(OSErr err, const char* msg=NULL);
  2477.  
  2478. inline void FailNULL(void* val, OSErr err, const char* msg=NULL) 
  2479.     if (val == 0) 
  2480.         Failure(err, msg); 
  2481. }
  2482.  
  2483. inline void FailError(OSErr err, const char* msg=NULL)
  2484. {
  2485.     if (err != kNoError)
  2486.         Failure(err, msg);
  2487. }
  2488.  
  2489. #define ErrorCode()                 (envExcept001.fError)
  2490. #define ErrorMessage()                (envExcept001.fMessage)
  2491. #define Volatile(x)                    ((void) &x)
  2492.  
  2493. #if qDebug
  2494. #define DebugFailure(err, msg)            Failure(err, msg)
  2495. #define DebugFailError(err, msg)        FailError(err, msg)
  2496. #define DebugFailNULL(ptr, err, msg)    FailNULL(ptr, err, msg)
  2497. #else
  2498. #define DebugFailure(err, msg)            Failure(err, NULL)
  2499. #define DebugFailError(err, msg)        FailError(err, NULL)
  2500. #define DebugFailNULL(ptr, err, msg)    FailNULL(ptr, err, NULL)
  2501. #endif
  2502.  
  2503.  
  2504. /*    -----------------------------------------------------------------
  2505.     The TRY/EXCEPT macros
  2506.     ----------------------------------------------------------------- */
  2507.  
  2508. #define TRY                                                \
  2509.         TException    envExcept001;                        \
  2510.         envExcept001.Push(&envExcept001);                \
  2511.         TException* envExcept002 = &envExcept001;        \
  2512.         if (!setjmp(envExcept001.fBuffer)) {            \
  2513.             do {
  2514.  
  2515. #define EXCEPT                                            \
  2516.             } while (0);                            \
  2517.             TException::Pop();        \
  2518.         }                                            \
  2519.         else                                        \
  2520.             for (; (1); Failure(envExcept001.fError))
  2521.  
  2522. /*******************************************************************************
  2523. ** Some inlines that rely on other inlines so we just do them here to preven
  2524. ** circular dependency problems.
  2525. ********************************************************************************/
  2526.  
  2527. /*    -------------------------------------------------------------------------
  2528.     Inline methods for TArray
  2529.     ------------------------------------------------------------------------- */
  2530.  
  2531.     inline TStandardPool* TArray::GetGrowPool() const
  2532.     {
  2533.         return (TStandardPool*)TMemoryPool::RecoverPool(fArray);
  2534.     }
  2535.  
  2536. #endif